/*
 * Copyright (c) 2006 - 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.util.concurrent.CountDownLatch;

/**
 * <p>Project: app2022 - Counter
 * <p>Powered by webrx On 2022-02-07 18:00:29
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class Counter {
    public static int count = 0;

    public synchronized static void inc() {
        try {
            Thread.sleep(10);
        } catch (Exception e) {
            e.printStackTrace();
        }
        count++;//count值自加1
    }

    public static void main(String[] args) throws InterruptedException {
        final CountDownLatch latch = new CountDownLatch(100);
        for (int i = 0; i < 100; i++) {
            new Thread(() -> {
                Counter.inc();
                latch.countDown();
            }).start();
        }
        latch.await();
        System.out.println("运行结果：" + Counter.count);
    }
}
